package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.HashMap;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Gui extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HashMap<String, String> minorScale = new HashMap<>();
	private HashMap<String, String> majorScale = new HashMap<>();
	private JTextField input;
	private JTextField output;
	private String musicNote;
	private JRadioButton minor;
	private JRadioButton major;

	public Gui() {
		// create GUI
		this.setTitle("Music Scale Generator");
		this.setSize(300, 300);
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		// create output and input field
		output = new JTextField(" ");
		output.setPreferredSize(new Dimension(300, 50));
		input = new JTextField("Music Note");
		input.addMouseListener(new Eventlistener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				input.setText("");

			}
		});
		// create the button for action
		JButton btn = new JButton("OK");
		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// if the button get pressed check which radiobutton is selected
				// and search then in the hashmap for the note and safe the
				// sale.
				// Then write the scale in the output field
				try {
					//if the first letter is a low case letter format it to an upper case letter
					musicNote = input.getText();
					char first = musicNote.charAt(0);
					if ((first <123) && (first >96) ){
						first = (char) (first - 32);
						musicNote = musicNote.replace(musicNote.charAt(0), first);
					}
					if (minor.isSelected() == true) {
						output.setText(minorScale.get(musicNote));
					} else if (major.isSelected()) {
						output.setText("Major");
						output.setText(majorScale.get(musicNote));
					} else {

					}

				} catch (Exception e2) {
					output.setText("Note not available");
				}

			}
		});

		JPanel radioButtonPanel = new JPanel(new FlowLayout());
		ButtonGroup radiobuttons = new ButtonGroup();

		minor = new JRadioButton("Minor:");
		major = new JRadioButton("Major:");
		radiobuttons.add(major);
		radiobuttons.add(minor);
		radioButtonPanel.add(major);
		radioButtonPanel.add(minor);

		this.add(input, BorderLayout.WEST);
		this.add(output, BorderLayout.SOUTH);
		this.add(btn, BorderLayout.EAST);
		this.add(radioButtonPanel, BorderLayout.NORTH);
		this.pack();

	}

	public void fillScales() {
		//create the hashmaps for the scales
		minorScale.put("C", "C - D - Eb - F - G - Ab - Bb - C");
		minorScale.put("D", "D - E - F - G - A - Bb - C - D");
		minorScale.put("E", "E - F# - G - A - B - C - D - E");
		minorScale.put("F", "F - G - Ab - Bb - C - Db - Eb - F");
		minorScale.put("G", "G - A - Bb - C - D - Eb - F - G");
		minorScale.put("A", "A - B - C - D - E - F - G - A");
		minorScale.put("B", "B - C# - D - E - F# - G - A - B");
		minorScale.put("C#", "C# - D# - E - F# - G# - A - B - C#");
		minorScale.put("Eb", "Eb - F - Gb - Ab - Bb - Cb - Db - Eb");
		minorScale.put("F#", "F# - G# - A - B - C# - D - E - F#");
		minorScale.put("G#", "G# - A# - B - C# - D# - E - F# - G#");
		minorScale.put("Bb", "Bb - C - Db - Eb - F - Gb - Ab - Bb");

		majorScale.put("C", "C - D - E - F - G - A - B - C");
		majorScale.put("D", "D - E - F# - G - A - B - C# - D");
		majorScale.put("E", "E - F# - G# - A - B - C# - D# - E");
		majorScale.put("F", "F - G - A - Bb - C - D - E - F");
		majorScale.put("G", "G - A - B - C - D - E - F# - G");
		majorScale.put("A", "A - B - C# - D - E - F# - G# - A");
		majorScale.put("B", "B - C# - D# - E - F# - G# - A# - B");
		majorScale.put("C#", "C# - D# - E# (=F) - F# - G# - A# - B# (=C) - C#");
		majorScale.put("Db", "Db - Eb - F - Gb - Ab - Bb - C - Db");
		majorScale.put("Eb", "Eb - F - G - Ab - Bb - C - D -Eb");
		majorScale.put("F#", "F# - G# - A# - B - C# - D# - E# (=F) - F#");
		majorScale.put("Gb", "Gb - Ab - Bb - Cb (=B) - Db - Eb - F - Gb");
		majorScale.put("Ab", "Ab - Bb - C - Db - Eb - F - G - Ab");
		majorScale.put("Bb", "Bb - C - D - Eb - F - G - A - Bb");
	}

}
